package com.ty.admin.controller;

import com.ty.common.constant.ResultMsgEnum;
import com.ty.common.utils.PageUtils;
import com.ty.common.utils.R;
import com.ty.common.validator.group.UpdateGroup;
import com.ty.skysystem.entity.CarriersEntity;
import com.ty.skysystem.service.CarriersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/** @author 天宇宇宇宇宇 @Data 2020/12/19 11:48 */
@RestController
@RequestMapping("accountant")
@Slf4j
@Api(tags = "运输成本核算模块 结算承运单，成本核算")
public class AccountantController {

  @Autowired private CarriersService carriersService;

  /** 查询出已接收承运单 */
  @GetMapping("receiveCarriers")
  @ApiOperation(value = "查询出已接收承运单", notes = "查询出已接收承运单", response = CarriersEntity.class)
  public R ReceiveCarriers(
      @ApiParam("map数组, 存放分页数据和动态sql条件") @RequestParam Map<String, Object> map) {
    PageUtils pageUtils = carriersService.receiveCarriers(map);
    return R.ok().put(pageUtils);
  }

  /** 录入成本 对承运单进行结算操作 */
  @GetMapping("settleAccount/{id}")
  @ApiOperation(
      value = "根据承运单ID进行合计成本计算,状态修改为已结算",
      notes = "根据承运单ID进行合计成本计算,状态修改为已结算",
      response = Integer.class)
  public R settleAccount(@ApiParam("承运单主键,从URI获取") @PathVariable("id") Integer carriersId) {
    return R.ok().put(carriersService.settleAccount(carriersId));
  }

  /** 查询出已结算承运单 */
  @GetMapping("settleAccountsCarriers")
  @ApiOperation(value = "查询出已结算承运单", notes = "查询出已结算承运单", response = CarriersEntity.class)
  public R settleAccountsCarriers(
      @ApiParam("map数组, 存放分页数据和动态sql条件") @RequestParam Map<String, Object> map) {
    PageUtils pageUtils = carriersService.queryPageHistoryCarriers(map);
    return R.ok().put(pageUtils);
  }
  /** 对已结算承运单进行修改成本结算操作 */
  @PatchMapping("/update")
  public R update(
          @ApiParam("承运单实体类") @RequestBody @Validated(UpdateGroup.class) CarriersEntity carriers) {
    return carriersService.updateById(carriers)
            ? R.ok(ResultMsgEnum.UPDATE_OK.getMsg())
            : R.error(ResultMsgEnum.UPDATE_ERROR.getMsg());
  }

  /** 查询已结算和已接收的承运任务 */
  @GetMapping("settleAccountsReceiveCarriers")
  @ApiOperation(value = "查询已结算和已接收的承运任务", notes = "查询已结算和已接收的承运任务", response = CarriersEntity.class)
  public R settleAccountsReceiveCarriers(
          @ApiParam("map数组, 存放分页数据和动态sql条件") @RequestParam Map<String, Object> map) {
    PageUtils pageUtils = carriersService.settleAccountsReceiveCarriers(map);
    return R.ok().put(pageUtils);
  }

  /** 核算运输成本 : 对已结算承运任务，按照车队为单位，在一定时间范围内的承运任务进行核算，以报表形式反馈 */
  @GetMapping("accountingForTransportationCosts")
  @ApiOperation(value = "查询对已结算承运任务，按照车队为单位", notes = "查询对已结算承运任务，按照车队为单位", response = CarriersEntity.class)
  public R accountingForTransportationCosts(
          @ApiParam("页码") @RequestParam(value = "page", defaultValue = "1") Integer page,
          @ApiParam("每页个数") @RequestParam(value = "limit", defaultValue = "5") Integer limit,
          @ApiParam("可根据车队名字进行筛选查询") @RequestParam(required = false) String teamName){
    PageUtils pageUtils = carriersService.accountingForTransportationCosts(page, limit, teamName);
    return R.ok().put(pageUtils);
  }

}
